var App = getApp();
// 格式化字典
function formatDict(code, value) {
  console.log("code", App.globalData.dictTree);
  var list = JSON.parse(JSON.stringify(App.globalData.dictTree));
  console.log("list", list);
  var res = list.filter(function (item) {
    return item.sKey == code;
  });
  var res2 = res[0].Child.filter(function (item) {
    return item.sKey == value;
  });
  console.log("res[0].Child", res[0].Child);
  return res2[0].sName;
}

// 处理接口返回数据
function handleResult(result) {
  let res = result;
  // 判断首层是否对象类型
  if (Object.prototype.toString.call(res) == "[object Object]") {
    // 判断对象非空
    if (Object.keys(res).length != 0) {
      for (const key in res) {
        if (Object.hasOwnProperty.call(res, key)) {
          // 判断值非空
          if (Object.prototype.toString.call(res[key]) == "[object Object]") {
            res[key] = handleResult(res[key]);
          } else if (
            res[key] === null ||
            res[key] === undefined ||
            res[key] === " "
          ) {
            res[key] = "--";
          }
        } else {
          console.log("key2", result[key]);
        }
      }
      console.log("res", res);
      return res;
    } else {
      return res;
    }
  } else if (
    Object.prototype.toString.call(res) == "[object Array]" &&
    res.length > 0
  ) {
    // 首层数组 内层存在对象
    for (let i = 0; i < res.length; i++) {
      if (Object.prototype.toString.call(res[i]) == "[object Object]") {
        res[i] = handleResult(res[i]);
      } else {
        res[i] = res[i];
      }
    }
    return res;
  } else {
    return res;
  }
}

module.exports = {
  formatDict: formatDict,
  handleResult: handleResult,
};
